package com.lwj.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwj.entity.Content;
import com.lwj.entity.ContentCategory;
import com.lwj.mapper.ContentMapper;
import com.lwj.service.IContentService;
import com.lwj.service.IContentCategoryService;
import com.lwj.util.PageResult;
import com.lwj.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwj
 * @since 2025-01-15
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {
    @Autowired
    private IContentCategoryService contentCategoryService;
    // 分页查询(不带条件)
    @Override
    public PageResult<Content> findByPage(Integer page, Integer pageSize) {
        Page<Content> page1 = this.page(new Page<>(page, pageSize));
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    //分页查询（带条件）
    @Override
    public PageResult<Content> search(Integer page, Integer pageSize, ContentVO vo) {
        //封装查询条件
        QueryWrapper<Content> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getTitle()), "title", vo.getTitle());
        wrapper.eq(vo.getCategoryId() != null && vo.getCategoryId()!= 0, "category_id", vo.getCategoryId());

        //根据wrapper包装好的条件进行分页查询
        Page<Content> page1 = this.page(new Page<>(page, pageSize), wrapper);


        page1.getRecords().forEach(st -> {
            ContentCategory contentCategory = contentCategoryService.getById(st.getCategoryId());
            st.setCname(contentCategory.getName());
        });


        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}
